﻿# Game.py: Alien Xenocide main module
# 04/06/09
# Copyright(c) 2009 Victor Freire e Cássio Espindola. All rights reserved.
# Python 2.6.2

import os
import sys
import pygame

import Sprites
import Gameobjects
import Timer
import Input
import Raycaster
import Maps
import Weapons
import Defines

class FPS_counter:
    def __init__(self):
        self.frames = 0
    def average(self):
        return self.frames / (pygame.time.get_ticks() / 1000.)



class Game:
    def __init__(self):
        ''' Initializes the game data and modules. '''
        # Pygame initialization
        pygame.init()
                
        # Set window caption and icon
        pygame.display.set_caption(Defines.WINDOW_TITLE)
        icon = pygame.image.load(os.path.join('data', 'sprites', 'ax.png'))
        if icon: pygame.display.set_icon(icon)
        else: warn("Failed to load window icon!")
    
        screen_flags = Defines.SCREEN_FLAGS
        if len(sys.argv) > 1 and sys.argv[1] == '-fullscreen':
            screen_flags = pygame.FULLSCREEN
        # Pygame display configuration
        try:
            self.screen = pygame.display.set_mode((Defines.SCREEN_WIDTH, Defines.SCREEN_HEIGHT), screen_flags, Defines.SCREEN_BPP)
        except pygame.error, msg:
            print "Error: failed to initialize display -", msg
                
        # Sprites Manager
        Sprites.Sprites.load_sprites()
        self.Weapons = Weapons.Weapons()
        
        # Raycasting renderer
        self.Renderer = Raycaster.RendererClass(Defines.PROJECTION_WIDTH, Defines.PROJECTION_HEIGHT)
        
        # The Player
        self.player = Gameobjects.Player((Defines.CUBE_SIZE*1.5, Defines.CUBE_SIZE*2.5), 45)
        
        # The Guns
        self.Weapons = Weapons.Weapons()
                
        # Setup map system
        Maps.Maps.load_maps()
        Maps.Maps.set_current_map('map2.dat')
       
                   
        # Game input
        self.Input = Input.Input()

        self.mult_factor = 0
               

    def shutdown(self):
        pygame.quit()
        

    def main(self):
        #DEBUG
##        monster = Gameobjects.Enemy('default', (200, 2000), ((200, 0), (200, 4098)))
##        monster2 = Gameobjects.Enemy('default', (200, 2000), ((0, 100), (4098, 100)))

        while 1:
            self.mult_factor = Timer.Timer.get_mult_factor()
            if not self.handle_input(self.mult_factor): break
            
            self.screen.fill((0, 0, 0)) #clear screen
            self.Renderer.render_scene(self.screen, self.player)
            self.Weapons.draw(self.screen)
            self.draw_hud()

            #DEBUG
##            monster.main(self.player, self.mult_factor)
##            monster2.main(self.player, self.mult_factor) 
##            pygame.draw.line(self.screen, (0, 255, 0), monster.patrol_limits[0], (200, 200))
##            self.screen.fill((255,0,0), (monster.x-10, int(monster.y*(200/4098.)), 20, 1))
##
##            pygame.draw.line(self.screen, (0, 255, 0), monster2.patrol_limits[0], (200, 100))
##            self.screen.fill((255,0,0), (int(monster2.x*(200/4098.)), monster2.y-10, 1, 20))
            #/DEBUG
            
            pygame.display.flip()
            Timer.Timer.update()
            

    def draw_hud(self):
        self.screen.blit(Sprites.Sprites.get_sprite('ui', 'barra_status'), (0, Defines.SCREEN_HEIGHT - 80))
    
                            
    def handle_input(self, mult_factor):
        keep_running = self.Input.get_input()
        
        if self.Input.check(pygame.K_ESCAPE):
            keep_running = False
        
                
        
        if self.Input.check(pygame.K_LSHIFT): mult_factor *= 2  # player running?
        
        if self.Input.check(pygame.K_UP):
            self.player.move_forward(mult_factor)
            
        if self.Input.check(pygame.K_DOWN):
            self.player.move_backward(mult_factor)
            
        if self.Input.check(pygame.K_LEFT):
            if self.Input.check(pygame.K_LALT):
                self.player.strafe_left(mult_factor)
            else:
                self.player.turn_left(mult_factor)

        if self.Input.check(pygame.K_RIGHT):
            if self.Input.check(pygame.K_LALT):
                self.player.strafe_right(mult_factor)
            else:
                self.player.turn_right(mult_factor)
        
        if self.Input.check(pygame.K_LCTRL):
            self.Weapons.fire()
        
        return keep_running


def main():
    try:
        import psyco
        psyco.full()
    except ImportError:
        print "Performance Warning: psyco not available"
        pass
    
    game = Game() # creates the game object and initializes the modules
    game.main()   # starts the game
    game.shutdown()   # cleans up before exitting
    print "Thanks for playing Alien Xenocide."    

main()
